using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Tic_Tac_Toe
{
    public partial class Form1 : Form
    {
        bool boolTimer = false; //declare a variable for timer
        bool boolPlayerTurn = true; // declare a variable for players turn

        public Form1()
        {
            InitializeComponent();                    
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Pen myPen = new Pen(Color.Black, 10); 
            //This will draw my Tic Tac Toe Board
            g.DrawLine(myPen, 0, 105, 320, 105);
            g.DrawLine(myPen, 0, 215, 320, 215);
            g.DrawLine(myPen, 105, 0, 105, 320);
            g.DrawLine(myPen, 215, 0, 215, 320);
            base.OnPaint(e);
        }

        private void btnQuit_Click(object sender, EventArgs e)
        {
            //This closes the application.
            Application.Exit(); 
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            //Clears and Enables Labels
            lblLeft.Text = " ";
            lblMiddle.Text = " ";
            lblRight.Text = " ";
            lblUpperLeft.Text = " ";
            lblUpperMiddle.Text = " ";
            lblUpperRight.Text = " ";
            lblLowerLeft.Text = " ";
            lblLowerMiddle.Text = " ";
            lblLowerRight.Text = " ";
            lblLeft.Enabled = true;
            lblMiddle.Enabled = true;
            lblRight.Enabled = true;
            lblUpperLeft.Enabled = true;
            lblUpperMiddle.Enabled = true;
            lblUpperRight.Enabled = true;
            lblLowerLeft.Enabled = true;
            lblLowerMiddle.Enabled = true;
            lblLowerRight.Enabled = true;
            radPlayer.Checked = true; // allows the player to start
            boolTimer = false;
            boolPlayerTurn = true; //allows the player control
        }

        private void lblUpperLeft_Click(object sender, EventArgs e)
        {
            if ((boolPlayerTurn == true) && (radPlayer.Checked == true))
            {
                //When the player clicks a square, the square is labeled with an X and disabled.
                //The radOpponent is checked to allow the oppenent's turn.
                lblUpperLeft.Text = "X";
                lblUpperLeft.Enabled = false;
                boolPlayerTurn = false; //diasables the player from clicking on another label
                Win();
                if (boolTimer == false) //this allows the timer to run.
                {
                    this.tmrTimer1.Start();
                }
            }
        }

        private void lblUpperMiddle_Click(object sender, EventArgs e)
        {
            if ((boolPlayerTurn == true) && (radPlayer.Checked == true))
            {
                //When the player clicks a square, the square is labeled with an X and disabled.
                //The radOpponent is checked to allow the oppenent's turn.
                lblUpperMiddle.Text = "X";
                lblUpperMiddle.Enabled = false;
                boolPlayerTurn = false; //diasables the player from clicking on another label
                Win();
                if (boolTimer == false) //this allows the timer to run.
                {
                    this.tmrTimer1.Start();
                }
            }
        }

        private void lblUpperRight_Click(object sender, EventArgs e)
        {
            if ((boolPlayerTurn == true) && (radPlayer.Checked == true))
            {
                //When the player clicks a square, the square is labeled with an X and disabled.
                //The radOpponent is checked to allow the oppenent's turn.
                lblUpperRight.Text = "X";
                lblUpperRight.Enabled = false;
                boolPlayerTurn = false; //diasables the player from clicking on another label
                Win();
                if (boolTimer == false) //this allows the timer to run.
                {
                    this.tmrTimer1.Start();
                }
            }
        }

        private void lblLeft_Click(object sender, EventArgs e)
        {
            if ((boolPlayerTurn == true) && (radPlayer.Checked == true))
            {
                //When the player clicks a square, the square is labeled with an X and disabled.
                //The radOpponent is checked to allow the oppenent's turn.
                lblLeft.Text = "X";
                lblLeft.Enabled = false;
                boolPlayerTurn = false; //diasables the player from clicking on another label
                Win();
                if (boolTimer == false) //this allows the timer to run.
                {
                    this.tmrTimer1.Start();
                }
            }
        }

        private void lblMiddle_Click(object sender, EventArgs e)
        {
            if ((boolPlayerTurn == true) && (radPlayer.Checked == true))
            {
                //When the player clicks a square, the square is labeled with an X and disabled.
                //The radOpponent is checked to allow the oppenent's turn.
                lblMiddle.Text = "X";
                lblMiddle.Enabled = false;
                boolPlayerTurn = false; //diasables the player from clicking on another label
                Win();
                if (boolTimer == false) //this allows the timer to run.
                {
                    this.tmrTimer1.Start();
                }
            }
        }

        private void lblRight_Click(object sender, EventArgs e)
        {
            if ((boolPlayerTurn == true) && (radPlayer.Checked == true))
            {
                //When the player clicks a square, the square is labeled with an X and disabled.
                //The radOpponent is checked to allow the oppenent's turn.
                lblRight.Text = "X";
                lblRight.Enabled = false;
                boolPlayerTurn = false; //diasables the player from clicking on another label
                Win();
                if (boolTimer == false) //this allows the timer to run.
                {
                    this.tmrTimer1.Start();
                }
            }
        }

        private void lblLowerLeft_Click(object sender, EventArgs e)
        {
            if ((boolPlayerTurn == true) && (radPlayer.Checked == true))
            {
                //When the player clicks a square, the square is labeled with an X and disabled.
                //The radOpponent is checked to allow the oppenent's turn.
                lblLowerLeft.Text = "X";
                lblLowerLeft.Enabled = false;
                boolPlayerTurn = false; //diasables the player from clicking on another label
                Win();
                if (boolTimer == false) //this allows the timer to run.
                {
                    this.tmrTimer1.Start();
                }
            }
        }

        private void lblLowerMiddle_Click(object sender, EventArgs e)
        {
            if ((boolPlayerTurn == true) && (radPlayer.Checked == true))
            {
                //When the player clicks a square, the square is labeled with an X and disabled.
                //The radOpponent is checked to allow the oppenent's turn.
                lblLowerMiddle.Text = "X";
                lblLowerMiddle.Enabled = false;
                boolPlayerTurn = false; //diasables the player from clicking on another label
                Win();
                if (boolTimer == false) //this allows the timer to run.
                {
                    this.tmrTimer1.Start();
                }
            }
        }

        private void lblLowerRight_Click(object sender, EventArgs e)
        {
            if ((boolPlayerTurn == true) && (radPlayer.Checked == true))
            {
                //When the player clicks a square, the square is labeled with an X and disabled.
                //The radOpponent is checked to allow the oppenent's turn.
                lblLowerRight.Text = "X";
                lblLowerRight.Enabled = false;
                boolPlayerTurn = false; //diasables the player from clicking on another label
                Win();
                if (boolTimer == false) //this allows the timer to run.
                {
                    this.tmrTimer1.Start();
                }
            }
        }

        private void radOpponent_CheckedChanged(object sender, EventArgs e)
        {
            Label[] arrLabel; //creates array of labels
            arrLabel = new Label[9] { this.lblLeft, 
                                      this.lblMiddle, 
                                      this.lblRight, 
                                      this.lblUpperLeft, 
                                      this.lblUpperMiddle, 
                                      this.lblUpperRight, 
                                      this.lblLowerLeft, 
                                      this.lblLowerMiddle, 
                                      this.lblLowerRight }; // fills the array with controls

            // main loop for opponent
            if ((boolPlayerTurn == false) & (radOpponent.Checked == true))
            {
                Random r = new Random();
                int intNum; //declare and intNum Variable

                //This will check to see if the square already has a value, if it does not it will label the square with a O.
                do
                {
                    intNum = r.Next(8); //random number from 0 to 8
                    if (arrLabel[intNum].Text == " ") //checks to see if label already has a value
                    {
                        arrLabel[intNum].Text = "O";
                        arrLabel[intNum].Enabled = false;
                        boolPlayerTurn = true;
                    }
                } while (boolPlayerTurn == false); // loops until boolPlayerTurn = true

                Win();
                radPlayer.Checked = true; //Gives control back to the user.
            }
        }
            //  I left this in here for you to see if this loop would have been OK. It has a break and worked fine.
            //
            //if (radOpponent.Checked == true)
            //{
            //    Random r = new Random();
            //    int intNum;//declare and intNum Variable
            //    intNum = r.Next(8);
            //    //This will check to see if the square already has a value, if it does not it will label the square with a O.
            //    do 
            //    {
            //        intNum= r.Next(8);
            //        if (arrLabel[intNum].Text == " ")
            //        {
            //            arrLabel[intNum].Text = "O";
            //            arrLabel[intNum].Enabled = false;
            //            break;
            //        }
            //    }          
            //    while (arrLabel[intNum].Enabled == false);
            //    Win();
            //   radPlayer.Checked = true;
            //}          

        protected void Win()
        {
            //This will check to see it there is a winner with X's, O's, or a draw

            if (lblLeft.Text == "X" && lblMiddle.Text == "X" && lblRight.Text == "X" ||
                lblUpperLeft.Text == "X" && lblUpperMiddle.Text == "X" && lblUpperRight.Text == "X" ||
                lblLowerLeft.Text == "X" && lblLowerMiddle.Text == "X" && lblLowerRight.Text == "X" ||
                lblLeft.Text == "X" && lblUpperLeft.Text == "X" && lblLowerLeft.Text == "X" ||
                lblMiddle.Text == "X" && lblUpperMiddle.Text == "X" && lblLowerMiddle.Text == "X" ||
                lblRight.Text == "X" && lblUpperRight.Text == "X" && lblLowerRight.Text == "X" ||
                lblUpperLeft.Text == "X" && lblMiddle.Text == "X" && lblLowerRight.Text == "X" ||
                lblUpperRight.Text == "X" && lblMiddle.Text == "X" && lblLowerLeft.Text == "X")
            {
                MessageBox.Show("You are the winner!!!", "Winner", MessageBoxButtons.OK);
                boolTimer = true; //disables the timer
            }

            else if (lblLeft.Text == "O" && lblMiddle.Text == "O" && lblRight.Text == "O" ||
                     lblUpperLeft.Text == "O" && lblUpperMiddle.Text == "O" && lblUpperRight.Text == "O" ||
                     lblLowerLeft.Text == "O" && lblLowerMiddle.Text == "O" && lblLowerRight.Text == "O" ||
                     lblLeft.Text == "O" && lblUpperLeft.Text == "O" && lblLowerLeft.Text == "O" ||
                     lblMiddle.Text == "O" && lblUpperMiddle.Text == "O" && lblLowerMiddle.Text == "O" ||
                     lblRight.Text == "O" && lblUpperRight.Text == "O" && lblLowerRight.Text == "O" ||
                     lblUpperLeft.Text == "O" && lblMiddle.Text == "O" && lblLowerRight.Text == "O" ||
                     lblUpperRight.Text == "O" && lblMiddle.Text == "O" && lblLowerLeft.Text == "O")
        
            {
                MessageBox.Show("Opponent is the winner!!!", "Winner", MessageBoxButtons.OK);
                boolTimer = true; //disables the timer
            }
            else if ((lblLeft.Text == "O" | lblLeft.Text == "X") & 
                     (lblMiddle.Text == "O" | lblMiddle.Text == "X") & 
                     (lblRight.Text == "O" | lblRight.Text == "X") &
                     (lblUpperLeft.Text == "O" | lblUpperLeft.Text == "X") & 
                     (lblUpperMiddle.Text == "O" | lblUpperMiddle.Text == "X") & 
                     (lblUpperRight.Text == "O" | lblUpperRight.Text == "X") &
                     (lblLowerLeft.Text == "O" | lblLowerLeft.Text == "X") &
                     (lblLowerMiddle.Text == "O" | lblLowerMiddle.Text == "X") & 
                     (lblLowerRight.Text == "O" | lblLowerRight.Text == "X"))
            {
                MessageBox.Show("Match is a draw!!!", "Winner", MessageBoxButtons.OK);
                boolTimer = true; //disables the timer
            }


        }

        private void tmrTimer1_Tick(object sender, EventArgs e)
        {
            this.tmrTimer1.Enabled = false; //disables the timer
            radOpponent.Checked = true; //starts the radOpponent_ CheckedChanged
        }
        
    }
}